﻿using System;
using System.Collections;
using System.Data;
using CE.Database;
using CE.Security;

/// <summary>
/// Summary description for CXAgentDB
/// </summary>
/// 
namespace CE.User
{
    public class CXAgentDB
    {
        public CXAgentDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Update(CXAgent cxagent)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocCXAgentStatusUpdateSingleItem";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@AgentId", cxagent.AgentId);
            _sPara.Add("@ProductType", cxagent.ProductType);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public DataTable GetCXAgentList(Int64 CEID, string ProductType, int JobStatus)
        {
            MyDatabase _Db = new MyDatabase();
            string _spName = "sprocCXAgentDetailSelectSingleItem";
            DataTable _result = null;
            SortedList _sPara = new SortedList();

            _sPara.Add("@CEID", CEID);
            _sPara.Add("@ProductType", ProductType);
            _sPara.Add("@JobStatus", JobStatus);

            _result = _Db.GetDataTableBySP(_spName, _sPara);

            return _result;
        }

        public CXAgent GetCXAgent(Int64 CEID, string ProductType, int JobStatus)
        {
            MyDatabase _Db = new MyDatabase();
            string _spName = "sprocCXAgentDetailSelectSingleItem";
            CXAgent _result = null;
            DataTable _dt;
            SortedList _sPara = new SortedList();

            _sPara.Add("@CEID", CEID);
            _sPara.Add("@ProductType", ProductType);
            _sPara.Add("@JobStatus", JobStatus);

            _dt = _Db.GetDataTableBySP(_spName, _sPara);

            if (_dt != null && _dt.Rows.Count > 0)
            {
                _result = new CXAgent();
                _result = FillDataRecord(_dt);
            }

            return _result;
        }

        private CXAgent FillDataRecord(DataTable myDataTable)
        {
            CXAgent myCXAgent = new CXAgent();
            myCXAgent.RefId = Convert.ToInt64(myDataTable.Rows[0]["RefId"].ToString());
            myCXAgent.AgentId = Convert.ToInt64(myDataTable.Rows[0]["AgentId"].ToString());
            myCXAgent.AgentName = myDataTable.Rows[0]["AgentName"].ToString();
            myCXAgent.Email = myDataTable.Rows[0]["Email"].ToString();
            myCXAgent.ProductType = myDataTable.Rows[0]["ProductType"].ToString();
            myCXAgent.JobStatus = Convert.ToInt32(myDataTable.Rows[0]["JobStatus"].ToString());
            myCXAgent.Status = Convert.ToBoolean(myDataTable.Rows[0]["Status"].ToString());
            return myCXAgent;
        }

    }
}